/**
 * Date: 2021/9/24
 * Author: january
 * Description: 
 */

#include <libecc/def.h>
#include <libecc/utils/utils.h>

/**
 * // 通用密钥, 即没有结构的随机字节
 * #define SEC_KEY_GENERIC 0x00000001
 * // ECC公钥
 * #define SEC_KEY_SM2_PUBKEY 0x00000002
 * // ECC私钥
 * #define SEC_KEY_SM2_PRIVKEY 0x00000003
 * // 通用类型公钥(subjectkeyinfo)
 * #define SEC_KEY_SUBPUBKEY 0x00000004
 * // 通用类型私钥
 * #define SEC_KEY_PKCS8 0x00000005
 * @param key_type_str
 * @return
 */

#define STR_EQU(s1, s2) (local_strcmp(s1, s2) == 0)
#define MACRO_TO_STR(m) #m

uint32_t secrypto_str_to_key_type(const char* key_type_str) {
    if (STR_EQU(key_type_str, MACRO_TO_STR(SEC_KEY_GENERIC))) {
        return SEC_KEY_GENERIC;
    } else if (STR_EQU(key_type_str, MACRO_TO_STR(SEC_KEY_SM2_PUBKEY))) {
        return SEC_KEY_SM2_PUBKEY;
    } else if (STR_EQU(key_type_str, MACRO_TO_STR(SEC_KEY_SM2_PRIVKEY))) {
        return SEC_KEY_SM2_PRIVKEY;
    } else if (STR_EQU(key_type_str, MACRO_TO_STR(SEC_KEY_SM2_PUBKEY))) {
        return SEC_KEY_SM2_PUBKEY;
    } else if(STR_EQU(key_type_str, MACRO_TO_STR(SEC_KEY_PKCS8))) {
        return SEC_KEY_PKCS8;
    } else {
        return SEC_UNDEFINE;
    }
}

const char* secrypto_key_type_to_str(uint32_t key_type) {
    switch (key_type) {
        case SEC_KEY_PKCS8: return MACRO_TO_STR(SEC_KEY_PKCS8);
        case SEC_KEY_SM2_PUBKEY: return MACRO_TO_STR(SEC_KEY_SM2_PUBKEY);
        case SEC_KEY_SM2_PRIVKEY: return MACRO_TO_STR(SEC_KEY_SM2_PRIVKEY);
        case SEC_KEY_SUBPUBKEY: return MACRO_TO_STR(SEC_KEY_SUBPUBKEY);
        case SEC_KEY_GENERIC: return MACRO_TO_STR(SEC_KEY_GENERIC);
        default: return MACRO_TO_STR(SEC_UNDEFINE);
    }
}

uint32_t secrypto_str_to_cipher_type(const char* type_str) {
    if (STR_EQU(type_str, MACRO_TO_STR(SEC_SM4_CBC))) {
        return SEC_SM4_CBC;
    } else if (STR_EQU(type_str, MACRO_TO_STR(SEC_SM4_CTR))) {
        return SEC_SM4_CTR;
    } else {
        return SEC_UNDEFINE;
    }
}

const char* secrypto_cipher_type_to_str(uint32_t type) {
    switch (type) {
        case SEC_SM4_CBC: return MACRO_TO_STR(SEC_SM4_CBC);
        case SEC_SM4_CTR: return MACRO_TO_STR(SEC_SM4_CTR);
        default: return MACRO_TO_STR(SEC_UNDEFINE);
    }
}

uint32_t secrypto_str_to_mac_type(const char* type_str) {
    if (STR_EQU(type_str, MACRO_TO_STR(SEC_HMAC_WITH_SM3))) {
        return SEC_HMAC_WITH_SM3;
    } else {
        return SEC_UNDEFINE;
    }
}

const char* secrypto_mac_type_to_str(uint32_t type) {
    switch (type) {
        case SEC_HMAC_WITH_SM3: return MACRO_TO_STR(SEC_HMAC_WITH_SM3);
        default: return MACRO_TO_STR(SEC_UNDEFINE);
    }
}

uint32_t secrypto_str_to_pkey_enc_type(const char* type_str) {
    if (STR_EQU(type_str, MACRO_TO_STR(SEC_SM2_ENC_WITH_SM3))) {
        return SEC_SM2_ENC_WITH_SM3;
    } else {
        return SEC_UNDEFINE;
    }
}

const char* secrypto_pkey_enc_type_to_str(uint32_t type) {
    switch (type) {
        case SEC_SM2_ENC_WITH_SM3: return MACRO_TO_STR(SEC_SM2_ENC_WITH_SM3);
        default: return MACRO_TO_STR(SEC_UNDEFINE);
    }
}

uint32_t secrypto_str_to_pkey_sign_type(const char* type_str) {
    if (STR_EQU(type_str, MACRO_TO_STR(SEC_SM2_SIG_WITH_SM3))) {
        return SEC_SM2_SIG_WITH_SM3;
    } else {
        return SEC_UNDEFINE;
    }
}

const char* secrypto_pkey_sign_type_to_str(uint32_t type) {
    switch (type) {
        case SEC_SM2_SIG_WITH_SM3: return MACRO_TO_STR(SEC_SM2_SIG_WITH_SM3);
        default: return MACRO_TO_STR(SEC_UNDEFINE);
    }
}


